package com.carrot.api.controller.system;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.carrot.common.core.controller.BaseController;
import com.carrot.common.core.domain.AjaxResult;
import com.carrot.common.core.domain.dto.TerminalInfoDto;
import com.carrot.common.core.domain.entity.SysStation;
import com.carrot.common.core.domain.entity.SysTerminalInfo;
import com.carrot.common.core.domain.vo.TerminalInfoVo;
import com.carrot.common.mybatis.Page;
import com.carrot.common.utils.StringUtils;
import com.carrot.common.utils.WrapperUtil;
import com.carrot.system.service.ISysStationService;
import com.carrot.system.service.ITerminalInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 点表配置
 *
 * @author 刘红真
 */
@RestController
@RequestMapping("/system/terminal")
public class SysTerminalInfoController extends BaseController {

    @Autowired
    private ITerminalInfoService terminalInfoService;

    @Autowired
    private ISysStationService stationService;

    /**
     * IEC104
     *
     * @param dto
     * @return
     */
    @PostMapping("list")
    public AjaxResult list(@RequestBody TerminalInfoDto dto) {

        Page<SysTerminalInfo, TerminalInfoVo> page = new Page<>(dto.getPageNum(), dto.getPageSize());

        QueryWrapper<SysTerminalInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(dto.getSearchContent()), "station_name", dto.getSearchContent());
        queryWrapper.eq(StringUtils.isNotEmpty(dto.getStationId()), "station_id", dto.getStationId());

        queryWrapper.orderByAsc("create_time");
        page.setQueryWrapper(queryWrapper);
        page = terminalInfoService.findPage(page);

        List<SysTerminalInfo> tList = page.getTList();
        List<TerminalInfoVo> vList = Lists.newArrayList();
        if (CollUtil.isNotEmpty(tList)) {
            List<String> stationIds = tList.stream().map(SysTerminalInfo::getStationId).collect(Collectors.toList());
            List<SysStation> stationList = stationService.query().in("station_id", stationIds).list();

            Map<String, String> stationMap = stationList.stream()
                    .collect(Collectors.toMap(SysStation::getStationId, SysStation::getStationName));

            for (SysTerminalInfo terminalInfo : tList) {
                TerminalInfoVo terminalInfoVo = WrapperUtil.transform(terminalInfo, TerminalInfoVo.class);
                terminalInfoVo.setStationName(stationMap.getOrDefault(terminalInfo.getStationId(), ""));
                vList.add(terminalInfoVo);
            }
        }
        page.setVList(vList);
        return AjaxResult.success(page.getPageDataVo2());
    }

    @GetMapping("detail")
    public AjaxResult detail(@RequestParam("id") Long id) {

        SysTerminalInfo terminalInfo = terminalInfoService.getById(id);
        return AjaxResult.success(WrapperUtil.transform(terminalInfo, TerminalInfoVo.class));
    }

    @PostMapping("add")
    public AjaxResult add(@RequestBody SysTerminalInfo terminalInfo) {

        terminalInfoService.addTerminalInfo(terminalInfo);
        return AjaxResult.success();
    }


    @PostMapping("update")
    public AjaxResult update(@RequestBody SysTerminalInfo terminalInfo) {

        terminalInfoService.updateTerminalInfo(terminalInfo);
        return AjaxResult.success();

    }

    @GetMapping("del")
    public AjaxResult del(@RequestParam("id") Long id) {

        terminalInfoService.delTerminalInfo(id);
        return AjaxResult.success();

    }


    @PostMapping("batchDel")
    public AjaxResult batchDel(@RequestBody TerminalInfoDto dto) {


        terminalInfoService.batchDelTerminalInfo(dto.getIds());
        return AjaxResult.success();

    }

}
